package com.isaac.sqlSession;

import com.isaac.pojo.Configuration;
import com.isaac.pojo.MappedStatement;

import java.beans.IntrospectionException;
import java.lang.reflect.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DefaultSqlSession implements SqlSession {
    private Configuration configuration;
    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... params) throws IllegalAccessException, IntrospectionException, InstantiationException, NoSuchFieldException, SQLException, InvocationTargetException, ClassNotFoundException {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        List<Object> list = simpleExecutor.query(configuration, mappedStatement, params);

        return (List<E>) list;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws IllegalAccessException, ClassNotFoundException, IntrospectionException, InstantiationException, SQLException, InvocationTargetException, NoSuchFieldException {
        List<Object> list = selectList(statementId, params);
        if(list.size()==1){
            return (T) list.get(0);
        }else {
            throw new RuntimeException("查询结果为空或返回结果过多");
        }
    }

    @Override
    public void insertOne(String statementId, Object... params) throws ClassNotFoundException, SQLException, IllegalAccessException, NoSuchFieldException {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        simpleExecutor.insert(configuration,mappedStatement,params);
    }

    @Override
    public void updateOne(String statementId, Object... params) throws ClassNotFoundException, SQLException, NoSuchFieldException, IllegalAccessException {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        simpleExecutor.update(configuration,mappedStatement,params);
    }

    @Override
    public void deleteOne(String statementId, Object... params) throws ClassNotFoundException, SQLException, NoSuchFieldException, IllegalAccessException {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        simpleExecutor.delete(configuration,mappedStatement,params);
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        Object o = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //  com.isaac.dao.IUserDao.selectOne
                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();
                // statementId
                String key = className + "." + methodName;
                Type genericReturnType = method.getGenericReturnType();

                if(methodName.contains("select")||methodName.contains("find")){
                    if (genericReturnType instanceof ParameterizedType) {
                        return selectList(key, args);
                    }
                    return selectOne(key, args);
                }else if(methodName.contains("save")||methodName.contains("insert")){
                    insertOne(key,args);
                    return null;
                }else if(methodName.contains("update")){
                    updateOne(key,args);
                    return null;
                }else if(methodName.contains("delete")){
                    deleteOne(key,args);
                    return null;
                }
                return null;
            }
        });

        return (T) o;


    }
}
